package com.digiproto.app_store.page;

import android.app.FragmentManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ListView;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.Indicators.PagerIndicator;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.digiproto.app_store.GameDetailActivity;
import com.digiproto.app_store.GameListAdapter;
import com.digiproto.app_store.model.Game;
import com.digiproto.app_store.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link IndexFragment#newInstance} factory method to
 * create an instance of this fragment.
 * <p>
 * Copyright (c) 2016 ycuzjj@163.com. All rights reserved.<br>
 */
public class IndexFragment extends Fragment implements BaseSliderView.OnSliderClickListener, ViewPagerEx.OnPageChangeListener{
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_ITEM_BG_COLOR_ID = "bg_color_id";
    private static final String TAG = IndexFragment.class.getSimpleName();

    // 轮播组件
    private SliderLayout mDemoSlider;
    private View view ;
    private int mBgColorId;
    // 所有游戏
    private ArrayList<Game> datas = null;
    // 每个游戏的swiper图片
    private ArrayList<String> swiperUrls = null;
    public IndexFragment() {
        // Required empty public constructor
    }
    // fragmentManager
    private FragmentManager manager;
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment MainTabFragment.
     */
    public static IndexFragment newInstance(FragmentManager fManager, ArrayList<Game> datas) {
        IndexFragment fragment = new IndexFragment();
        fragment.setManager(fManager);
        fragment.setDatas(datas);
        Bundle args = new Bundle();
        // args.putInt(ARG_ITEM_BG_COLOR_ID, colorId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // View
        view = inflater.inflate(R.layout.index, container, false);
        if(view != null){
            initSwiper(inflater);
        }
        return view;
    }

    private void initSwiper(LayoutInflater inflater){
        // 数据准备
        /**
        HashMap<String,Integer> file_maps = new HashMap<String, Integer>();
        file_maps.put("Hannibal",R.drawable.hannibal);
        file_maps.put("Big Bang Theory",R.drawable.bigbang);
        file_maps.put("House of Cards",R.drawable.hannibal);
        file_maps.put("Game of Thrones", R.drawable.bigbang);
        */
        // 首页swiper图片
        HashMap<String,String> file_maps = new HashMap<String, String>();
        file_maps.put("智能积木1","http://img07.mifile.cn/v1/MI_7230F3A58BDCB/9bb80596f4bbeefb3036973726b2f75f.jpg");
        file_maps.put("智能积木2","http://img08.mifile.cn/v1/MI_7230F3A58BDCB/72d93e8d1a3dd6a20fff848b727fdf2c.jpg");
        file_maps.put("智能积木3","http://img06.mifile.cn/v1/MI_7230F3A58BDCB/4df05c086d623ee6c1644f45a0e6c38a.jpg");
        file_maps.put("智能积木4","http://img08.mifile.cn/v1/MI_7230F3A58BDCB/72d93e8d1a3dd6a20fff848b727fdf2c.jpg");
        // slider
        mDemoSlider = (SliderLayout) view.findViewById(R.id.slider);

        for(String name : file_maps.keySet()){
            TextSliderView textSliderView = new TextSliderView(this.getContext());
            // initialize a SliderLayout
            textSliderView
                    .description(name)
                    .image(file_maps.get(name))
                    .setScaleType(BaseSliderView.ScaleType.Fit)
                    .setOnSliderClickListener(this);

            //add your extra information
            textSliderView.bundle(new Bundle());
            textSliderView.getBundle()
                    .putString("extra",name);
            mDemoSlider.addSlider(textSliderView);
        }

        mDemoSlider.setPresetTransformer(SliderLayout.Transformer.Accordion);
        mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mDemoSlider.setCustomAnimation(new DescriptionAnimation());
        mDemoSlider.setDuration(4000);
        mDemoSlider.addOnPageChangeListener(this);

        // 动态加载头部
        View headview = inflater.inflate(R.layout.item_head,null,false);
        ListView l = (ListView)view.findViewById(R.id.transformers);

        // 写死的数据
        datas = new ArrayList<Game>();
        swiperUrls = new ArrayList<>();

        List<String> imgUrls = new ArrayList<>();
        imgUrls.add("http://odlp0qtdb.bkt.clouddn.com/page1.png");
        imgUrls.add("http://odlp0qtdb.bkt.clouddn.com/page2.png");
        imgUrls.add("http://odlp0qtdb.bkt.clouddn.com/page3.png");
        imgUrls.add("http://odlp0qtdb.bkt.clouddn.com/page4.png");
        imgUrls.add("http://odlp0qtdb.bkt.clouddn.com/page11.png");

        String gameIconUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1512017930656&di=37ac7056d5828d53e523bfcc13661c04&imgtype=0&src=http%3A%2F%2Fimg.25pp.com%2Fuploadfile%2Fapp%2Ficon%2F20160525%2F1464169896131456.jpg";
        for(int i = 1;i <= 10;i++){
            Game game = new Game(String.valueOf(i),imgUrls,gameIconUrl,"俄罗斯砖块","益智游戏","9988","2.2M");
            datas.add(game);
        }

        // 设置头部
        l.addHeaderView(headview);
        // 游戏列表适配器
        l.setAdapter(new GameListAdapter(datas,getActivity()));
        // 设置监听器
        l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 传递数据对象
                Bundle bundle = new Bundle();
                // 设置传递的数据
                bundle.putSerializable("selectGame",datas.get(position));
                // 将数据设置到参数上
                // gdf.setArguments(bddata);
                // 获取Activity控件
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(IndexFragment.this.getActivity(), GameDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onStop() {
        mDemoSlider.stopAutoCycle();
        super.onStop();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_custom_indicator:
                mDemoSlider.setCustomIndicator((PagerIndicator) getView().findViewById(R.id.custom_indicator));
                break;
            case R.id.action_custom_child_animation:
                mDemoSlider.setCustomAnimation(new ChildAnimationExample());
                break;
            case R.id.action_restore_default:
                mDemoSlider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
                mDemoSlider.setCustomAnimation(new DescriptionAnimation());
                break;
            case R.id.action_github:
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/daimajia/AndroidImageSlider"));
                startActivity(browserIntent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSliderClick(BaseSliderView slider) {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public FragmentManager getManager() {
        return manager;
    }

    public void setManager(FragmentManager manager) {
        this.manager = manager;
    }

    public ArrayList<Game> getDatas() {
        return datas;
    }

    public void setDatas(ArrayList<Game> datas) {
        this.datas = datas;
    }
}